.PHONY :all
.PHONY :clean

SNAPPY_TAR = snappy-1.0.3.tar.gz
LEVELDB_TAR = leveldb.tar.bz2
THRIFT_TAR = thrift-0.7.0.tar.gz
GTEST_TAR = gtest-1.6.0.zip
CURL_TAR = curl-7.23.1.tar.bz2 
BLOOM_TAR = bloom.tar.bz2 

SNAPPY_LIB = install/lib/libsnappy.a
LEVELDB_LIB = install/lib/libleveldb.a
THRIFT_LIB = install/lib/libthrift.a
GTEST_LIB = install/lib/libgtest_main.a install/lib/libgtest.a
CURL_LIB = install/lib/libcurl.a
BLOOM_FILES = install/include/bloom/bloom_filter.hpp

all: $(SNAPPY_LIB) $(THRIFT_LIB) $(LEVELDB_LIB) \
     $(GTEST_LIB) $(CURL_LIB) $(BLOOM_FILES)

$(SNAPPY_LIB) : $(SNAPPY_TAR)
	@echo "========building snappy========"
	@echo "tar xf snappy-1.0.3.tar.gz"  && \
	tar xf snappy-1.0.3.tar.gz && \
	echo "cd snappy-1.0.3" && \
	cd snappy-1.0.3 && \
	echo "./configure --prefix=$$(pwd)/../install/ --disable-shared" && \
	./configure --prefix=$$(pwd)/../install/ --disable-shared >> ../log && \
	echo "make" && \
	make >> ../log && \
	echo "make install" && \
	make install >> ../log 
	@if [ $$? -eq 0 ]; then \
		echo "========successfully built snappy========"; \
	else \
		echo "========failed to build snappy========"; \
	fi
	@echo

$(LEVELDB_LIB) : $(SNAPPY_LIB) $(LEVELDB_TAR)
	@echo "========building leveldb========"
	@echo "tar xf leveldb.tar.bz2"  && \
	tar xf leveldb.tar.bz2  && \
	echo "cd leveldb" && \
	cd leveldb && \
	echo "make USER_CFLAGS=\"-I../install/include -L../install/lib\"" && \
	make USER_CFLAGS="-I../install/include -L../install/lib"  >> ../log && \
	echo "install leveldb to install" && \
	cp -a include/leveldb ../install/include/ && \
	cp libleveldb.a ../install/lib/
	@if [ $$? -eq 0 ]; then \
		echo "========successfully built leveldb========"; \
	else \
		echo "========failed to build leveldb========"; \
	fi
	@echo

$(THRIFT_LIB) : $(THRIFT_TAR)
	@echo "========building thrift========"
	@echo "tar xf thrift-0.7.0.tar.gz"  && \
	tar xf thrift-0.7.0.tar.gz && \
	echo "cd thrift-0.7.0" && \
	cd thrift-0.7.0 && \
	echo "./configure --prefix=$$(pwd)/../install/ \n \
		--disable-shared --without-java --without-python \n \
		--without-ruby --disable-shared" && \
	sh ./configure --prefix=$$(pwd)/../install/ \
		--disable-shared --without-java --without-python \
		--without-ruby --without-php --disable-shared >> ../log && \
	echo "make" && \
	make >> ../log && \
	echo "make install" && \
	make install >> ../log 
	@if [ $$? -eq 0 ]; then \
		echo "========successfully built thrift========"; \
	else \
		echo "========failed to build thrift========"; \
	fi

$(GTEST_LIB) : $(GTEST_TAR) 
	@echo "========building gtest========" && \
	unzip gtest-1.6.0.zip && \
	cd gtest-1.6.0/make && \
	make  gtest_main.a gtest.a >> ../../log && \
	test -d install/include || mkdir -p install/include && \
	test -d install/lib || mkdir -p install/lib && \
	cp -f gtest_main.a ../../install/lib/libgtest_main.a && \
	cp -f gtest.a ../../install/lib/libgtest.a && \
	cp -af ../include/gtest ../../install/include

$(CURL_LIB) : $(CURL_TAR)
	@echo "========building curl========"
	tar xf curl-7.23.1.tar.bz2
	cd curl-7.23.1 && \
	sh ./configure --prefix=$$(pwd)/../install/ --without-libidn \
		--disable-shared --disable-ldap --disable-ldaps >> ../log && \
	make >> ../log && \
	make install >> ../log 
	@if [ $$? -eq 0 ]; then \
		echo "========successfully built curl========"; \
	else \
		echo "========failed to build curl========"; \
	fi

$(BLOOM_FILES) : $(BLOOM_TAR)
	@echo "========building bloom========"
	tar xf $^
	cd bloom && make
	rm -fr install/include/bloom
	mv bloom install/include/
	@echo "========successfully built bloom========"; \

clean:tidy
	rm -fr install/*
tidy:
	rm -fr snappy-1.0.3
	rm -fr leveldb
	rm -fr thrift-0.7.0
	rm -fr gtest-1.6.0
	rm -fr curl-7.23.1
	rm -fr log

